package com.wjtc.common.base.restful;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class WebPage {
	private Integer limit;   //页面大小
		private Integer offset;  //页码
		/**
		 * 查询参数
		 */
		Map<String, Object>	params	= null;
	
		public Integer getLimit() {
	
			return limit;
		}
	
		public void setLimit(Integer limit) {
	
			this.limit = limit;
		}
	
		public Integer getOffset() {
	
			return offset;
		}
	
		public void setOffset(Integer offset) {
	
			this.offset = offset;
		}
	
		public Map<String, Object> getParams() {
	
			return params;
		}
	
		public void setParams(Map<String, Object> params) {
	
			this.params = params;
		}
	
		public Integer getPageNumber() {
			if(limit!=null && offset!=null && limit>0){
				if(offset == 0){
					return 1;
				}else{
					return offset != 0 ? limit/offset+1 : limit/offset;
				}
			}
			return 1;
		}
	
		/**
		 * @Description: 转换传过来的需要查询的参数直接放入map
		 */
		public Map<String, Object> castQueryObject(Object obj) throws Exception {
			if(params == null){
				params = new HashMap<String, Object>();
			}
			Field[] fs = obj.getClass().getDeclaredFields();
			for (Field f : fs) {
				f.setAccessible(true);
				String name = f.getName();
				params.put(name, f.get(obj));
			}
			return params;
		}
}

